package com.adm.biz;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.yfsoft.comm.util.BizResult;
import org.yfsoft.comm.util.StringUtil;

import com.adm.ctl.ResultType;
import com.adm.dao.CommonDao;

public class ModelService extends Service {

	private CommonDao dao = null;

	public void setDao( CommonDao dao) {
		this.dao = dao;
	}
	
	public BizResult list(Map<String,String> input){
		BizResult rst = new BizResult().setCode("0");
		this.setReqAttr("models", this.dao.list("sys_model",null,null,null,100,0).getData());
		rst.setType(String.valueOf(ResultType.REDIRECT));
		rst.setPage("model/model.jsp");
		return rst;
	}
	
	public BizResult field(Map<String,String> input){
		BizResult rst = new BizResult().setCode("0");
		rst.setType(String.valueOf(ResultType.REDIRECT));
		this.setReqAttr("fields", this.dao.desc(input.get("t")).getData());
		rst.setPage("model/field.jsp");
		return rst;
	}
	
	public BizResult removerelation(Map<String,String> input){
		String m_id = input.get("m_id");
		this.dao.delete("sys_model_control", "mc_model_id = "+m_id);
		Map<String,String> e = new HashMap<String,String>();
		e.put("m_relation","0");
		this.dao.update(e, "m_id = "+m_id, "sys_model");
		return list(input);
	}
	
	public BizResult addreleation(Map<String,String> input){
		int pos = -1;
		String i,c ;
		Map<String,Map<String,String>> es = new HashMap<String,Map<String,String>>();
		Map<String,String> e;
		for(String k : input.keySet()){
			pos = k.indexOf("~");
			if(pos<0) continue;
			i = StringUtil.between(k, "~", StringUtil.BetweenPos.END);
			if(!es.containsKey(i)){
				e = new HashMap<String,String>();
				es.put(i, e);
			}
			e = es.get(i);
			c = StringUtil.between(k, "~", StringUtil.BetweenPos.START);
			e.put(c, input.get(k));
		}
		i = input.get("m_id");
		for(String k : es.keySet()){
			e = es.get(k);
			e.put("mc_model_id", i);
			this.dao.insert(e, "sys_model_control");
		}
		e = new HashMap<String,String>();
		e.put("m_relation","1");
		this.dao.update(e, "m_id = "+i, "sys_model");
		return list(input);
	}
	
	public BizResult control(Map<String,String> input){
		BizResult rst = this.dao.query("sys_model", new String[]{"m_id","m_name","m_title","m_view"}, "m_id = "+input.get("id"));
		Map<String,Object> model = (Map<String,Object>)rst.getData();
		this.setReqAttr("fields", this.dao.desc(model.get("m_view").toString()).getData());
		this.setReqAttr("model", model);
		
		//********************* Start Load the controls *********************
		{
			Object obj = this.dao.list("sys_control", null, null,null, 100, 0).getData();
			StringBuffer sb = new StringBuffer();
			List<Map<String,Object>> datas = (List<Map<String,Object>>)obj;
			for(Map<String,Object> d : datas){
				sb.append("<option value='").append(d.get("c_id")).append("' >").append(d.get("c_title")).append("</option>");
			}
			this.setReqAttr("options", sb.toString());
		}
		//********************* End Load the controls *********************
		
		//********************* Start Load the data_source *********************
		{
			Object obj = this.dao.list("sys_ds", null, null,null, 100, 0).getData();
			StringBuffer sb = new StringBuffer("<option value='-1'>请选择</option>");
			List<Map<String,Object>> datas = (List<Map<String,Object>>)obj;
			for(Map<String,Object> d : datas){
				sb.append("<option value='").append(d.get("ds_id")).append("' >").append(d.get("ds_name")).append("</option>");
			}
			this.setReqAttr("datasets", sb.toString());
		}
		//********************* End Load the data_source *********************
		
		rst.setType(String.valueOf(ResultType.REDIRECT));
		rst.setPage("model/relation.jsp");
		return rst;
	}
	
	public BizResult table(Map<String,String> input){
		BizResult rst = new BizResult().setCode("0");
		rst.setType(String.valueOf(ResultType.REDIRECT));
		this.setReqAttr("tables", this.dao.table().getData());
		rst.setPage("model/table.jsp");
		return rst;
	}
	
	public BizResult add(Map<String,String> input){
		this.dao.insert(input, "sys_model");
		return list(input);
	}

	public BizResult del(Map<String,String> input){
		BizResult rst = new BizResult().setCode("0");
		rst.setType(String.valueOf(ResultType.REDIRECT));
		rst.setPage("model/add.jsp");
		return rst;
	}
	
	public BizResult addform(Map<String,String> input){
		String t = input.get("t");
		this.setReqAttr("table", t);
		BizResult rst = new BizResult().setCode("0");
		rst.setType(String.valueOf(ResultType.REDIRECT));
		rst.setPage("model/add.jsp");
		return rst;
	}
	
	public BizResult updform(Map<String,String> input){
		//TODO..获取值
		BizResult rst = new BizResult().setCode("0");
		rst.setType(String.valueOf(ResultType.REDIRECT));
		rst.setPage("model/upd.jsp");
		return rst;
	}
}
